<!-- 报警规则页面 -->
<template>
  <div class="ele-body">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>设备管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-circle-plus-outline" @click="openEdit(null)">添加</el-button>
      </div>
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="井号:">
              <el-select
                filterable
                clearable
                v-model="where.device_id"
                placeholder="请选择井号">
                <el-option v-for="item in deviceList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 400px)">
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:alarmrules:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此报警规则吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:alarmrules:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>

        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            :inactive-value="2"/>
        </template>

        <!-- 报警等级列 -->
        <template slot="alarm_level" slot-scope="{row}">
          <el-tag
            :type="['info', 'warning', 'danger'][row.alarm_level-1]"
            size="mini">
            {{ ['1级', '2级', '3级'][row.alarm_level - 1] }}
          </el-tag>
        </template>

      </ele-pro-table>

      <!-- 编辑弹窗 -->
      <alarmrules-edit
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>报警记录</span>
      </div>
      <el-form
        :model="where2"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload2"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="报警参数:">
              <el-select
                clearable
                v-model="where2.alarm_param"
                placeholder="请选择报警参数">
                <el-option value="liuya" label="流压"></el-option>
                <el-option value="biao_liuya" label="校准流压"></el-option>
                <el-option value="jingdi_temp" label="井底温度"></el-option>
                <el-option value="taoya" label="套压"></el-option>
                <el-option value="yezhu_high" label="液柱高度"></el-option>
                <el-option value="shunshi_qiliang" label="瞬时气量"></el-option>
                <el-option value="liuliang_yali" label="流量计压力"></el-option>
                <el-option value="liuliang_temp" label="流量计温度"></el-option>
                <el-option value="tran_speed" label="转速"></el-option>
                <el-option value="pinlv" label="频率"></el-option>
                <el-option value="dianliu" label="电流"></el-option>
                <el-option value="dianya" label="电压"></el-option>
                <el-option value="chanshui" label="产水"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="报警等级:">
              <el-select
                clearable
                v-model="where2.alarm_level"
                placeholder="请选择报警等级">
                <el-option value="1" label="1级"></el-option>
                <el-option value="2" label="2级"></el-option>
                <el-option value="3" label="3级"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="井号:">
              <el-select
                filterable
                clearable
                v-model="where2.device_id"
                placeholder="请选择井号">
                <el-option v-for="item in deviceList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload2">查询
              </el-button>
              <el-button @click="reset2">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table2"
        :where="where2"
        :datasource="url2"
        :columns="columns2"
        :selection.sync="selection2"
        height="calc(100vh - 400px)">

        <!-- 操作列 -->
        <template slot="action2" slot-scope="{row}">
          <el-popconfirm
            class="ele-action"
            title="确定要删除此报警规则吗？"
            @confirm="remove2(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:alarmlog:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>

        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch2"
            v-if="permission.includes('sys:alarmlog:dall')">删除
          </el-button>
        </template>

        <!-- 报警等级列 -->
        <template slot="alarm_level" slot-scope="{row}">
          <el-tag
            :type="['info', 'warning', 'danger'][row.alarm_level-1]"
            size="mini">
            {{ ['1级', '2级', '3级'][row.alarm_level - 1] }}
          </el-tag>
        </template>

      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import AlarmrulesEdit from "@/views/alarmrules/alarmrules-edit";

export default {
  name: 'AlarmRules',
  components: {AlarmrulesEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/alarmrules/index',
      // 表格列配置
      columns: [
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'param_name',
          label: '报警参数',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'relationship',
          label: '计算',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'threshold_start',
          label: '临界值1',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'threshold_end',
          label: '临界值2',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'param_units',
          label: '参数单位',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'alarm_level',
          label: '报警等级',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot: 'alarm_level'
        },
        {
          prop: 'device_name',
          label: '井号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,

      // 表格数据接口
      url2: '/alarmlog/index',
      // 表格列配置
      columns2: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'param_name',
          label: '报警参数',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'value',
          label: '报警值',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'alarm_level',
          label: '报警等级',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot: 'alarm_level'
        },
        {
          prop: 'device_name',
          label: '井号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '报警时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action2',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where2: {},
      // 表格选中数据
      selection2: [],
      // 当前编辑数据
      current2: null,
      // 是否显示编辑弹窗
      showEdit2: false,
      deviceList:[],
      device_types: [
        { label: '杆式泵', value: '1' },
        { label: '螺杆泵', value: '2' },
        { label: '柱塞泵', value: '3' },
      ],
    };
  },
  created() {
    this.getDeviceList()
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },

    /* 刷新表格 */
    reload2() {
      this.$refs.table2.reload({where: this.where2});
    },

    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },

    /* 重置搜索 */
    reset2() {
      this.where2 = {};
      this.reload2();
    },

    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/alarmrules/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的报警规则吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/alarmrules/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 报警记录删除 */
    remove2(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/alarmlog/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload2();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    /* 报警记录批量删除 */
    removeBatch2() {
      if (!this.selection2.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的报警记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/alarmlog/delete', {id: this.selection2.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload2();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/alarmrules/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    getDeviceList(){
      this.$http.post('/device/all').then(res => {
        if (res.data.code === 0) {
          this.deviceList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}
</style>
